package com.renshengjiwu.utils;

/**
 * Created by Administrator on 2016/4/20.
 */
public class HashPassword {

  private static final int INTERATIONS = 1024;

  private static final int SALT_SIZE = 8;

  public String salt;

  public String password;

  // 密码加密
  public static HashPassword encryptPassword(String plainPassword) {
    HashPassword result = new HashPassword();
    byte[] salt = Digests.generateSalt(SALT_SIZE);
    result.salt = Encodes.encodeHex(salt);
    byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, INTERATIONS);
    result.password = Encodes.encodeHex(hashPassword);
    return result;
  }

  @Override
  public String toString() {
    return "HashPassword [salt=" + salt + ", password=" + password + "]";
  }

  public static boolean validatePassword(String plainPassword, String password, String salt) {
    byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), Encodes.decodeHex(salt), INTERATIONS);
    String oldPassword = Encodes.encodeHex(hashPassword);
    return password.equals(oldPassword);
  }

  public static void main(String[] args) {

    HashPassword hashPassword = HashPassword.encryptPassword("123456");
    // String hashAlgorithmName = "SHA1";
    // Object credentials = "123456";
    // Object salt = ByteSource.Util.bytes("admin");;
    // int hashIterations = 1024;
    // System.out.println(salt);
    // Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
    System.out.println(hashPassword);
  }

}
